

#include"head.h"
struct Filedata
{
    char name[32];
    int size;
    char str[128];
    int flag;
};
    
int main(int argc,char *argv[])
{
    if(argc != 3 )
    {
        printf("Usage %s  ip, port\n",argv[0]);
        return -1;
    }
    struct Filedata data;
    int sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd < 0)
    {
        perror("socket");
        pthread_exit(NULL);
    }
    
    struct sockaddr_in srcaddr;
    srcaddr.sin_family          = AF_INET;
    srcaddr.sin_port            = htons(atoi(argv[2]));
    srcaddr.sin_addr.s_addr     = inet_addr(argv[1]);
    socklen_t len               = sizeof(struct sockaddr_in);
    struct sockaddr_in myaddr;
    myaddr.sin_family          = AF_INET;
    myaddr.sin_port            = htons(4444);
    myaddr.sin_addr.s_addr     = inet_addr("172.16.3.46");
    int re = bind(sockfd,(struct sockaddr *)&myaddr,len);
    if( re < 0 )
    {
        perror("bind error");
        return -1;
    }

    int num =0;
    // FILE *ffp[32];

    while(1)
    {
        char buff[128] = {0};
        printf("请输入你要发送的文件名：");
        // fgets(buff,sizeof(buff),stdin);
        scanf("%s",data.name);
        int count[128] = {0};
        /*printf("sss =%c\n",buff[strlen(buff)-2]);
        for(int i = 0;i < strlen(buff);i ++)
        {
        if((buff[i] ==' ' && buff[i+1] != ' '))
        {
        count[num+1] = i;
        num ++;
    }
        if((i == strlen(buff)-2) && buff[i] != ' ')
        {
        num++;
        count[num+1] = i;
    }
    }*/
        /* for(int k=0;k<num;k++)
        printf("file =...%d...\n",count[k]+1);
        //char filename[64][32] = {0};*/
	
        printf("size = %lu\n",sizeof(data.name)+sizeof(data.size));
        printf("发送的文件为%s\n",data.name);
        FILE *ffp=fopen(data.name,"r");
        if(ffp==NULL)
        {
            perror("fopen");
            continue;
        }

        data.flag = 0;
        while(1)
        {
            data.size = fread(data.str,sizeof(char),sizeof(data.str),ffp);
            //strcpy(data.name,buff);
            int ret = sendto(sockfd,&data,sizeof(struct Filedata),0,(struct sockaddr*)&srcaddr,len);
            if(ret < 0)
            {
                perror("sendtofile error");
                break;;
            }
            printf("len =%d\n",data.size);
            if(data.size == 0)
            {
                printf("传送完毕\n");
                break;
            }
            bzero(&data,sizeof(struct Filedata));
            data.flag = 1;
        }
	char recvbuf[8]={0};
        recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&srcaddr,&len);
        printf("文件发送完毕\n");
        bzero(&data,sizeof(data));
        fclose(ffp);
        bzero(buff,sizeof(buff));
        num =0;
        }


}
